//
//  ZJHSessionConfiguration.m
//  ZJHURLProtocol
//
//  Created by ZhangJingHao2345 on 2018/8/24.
//  Copyright © 2018年 ZhangJingHao2345. All rights reserved.
//

#import "ZJHSessionConfiguration.h"
#import <objc/runtime.h>
#import "ZJHURLProtocol.h"
#import "YYModel.h"
#import "WRNSUserDefaultsSave.h"

@implementation ZJHSessionConfiguration



+ (ZJHSessionConfiguration *)defaultConfiguration {
    static ZJHSessionConfiguration *staticConfiguration;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        staticConfiguration=[[ZJHSessionConfiguration alloc] init];
    });
    return staticConfiguration;
}

- (NSMutableArray *)larray {
    
    if (_larray == nil) {
        
        _larray = [NSMutableArray array];
    }
    
    return _larray;
}

- (NSMutableArray<UrlModel *> *)yarray {
    
    if (_yarray == nil) {
        
        _yarray = [NSMutableArray array];
    }
    
    return _yarray;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.isSwizzle = NO;
    }
    return self;
}

- (void)load {
    self.isSwizzle=YES;
    Class cls = NSClassFromString(@"__NSCFURLSessionConfiguration") ?: NSClassFromString(@"NSURLSessionConfiguration");
    [self swizzleSelector:@selector(protocolClasses) fromClass:cls toClass:[self class]];
    
}

- (void)unload {
    self.isSwizzle=NO;
    Class cls = NSClassFromString(@"__NSCFURLSessionConfiguration") ?: NSClassFromString(@"NSURLSessionConfiguration");
    [self swizzleSelector:@selector(protocolClasses) fromClass:cls toClass:[self class]];
}

- (void)swizzleSelector:(SEL)selector fromClass:(Class)original toClass:(Class)stub {
    Method originalMethod = class_getInstanceMethod(original, selector);
    Method stubMethod = class_getInstanceMethod(stub, selector);
    if (!originalMethod || !stubMethod) {
        [NSException raise:NSInternalInconsistencyException format:@"Couldn't load NEURLSessionConfiguration."];
    }
    method_exchangeImplementations(originalMethod, stubMethod);
}

- (NSArray *)protocolClasses {
    // 如果还有其他的监控protocol，也可以在这里加进去
    return @[[ZJHURLProtocol class]];
}


#pragma mark - 储存数据
+ (void)saveUrlModel:(NSMutableArray <UrlModel *>*)arrUrlModel {
    
    NSData *data = [arrUrlModel yy_modelToJSONData];
    
    [WRNSUserDefaultsSave saveInformation:data forKey:@"yy_modelToJSONData"];
    
}

///获取当前已经存储的数据
+ (NSMutableArray <UrlModel *>*)getUrlModel {
    
    NSData *data = [WRNSUserDefaultsSave getFriendInformation:@"yy_modelToJSONData"];
 
    id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
    NSArray *arr = (NSArray *)jsonObject;
    
    return arr.mutableCopy;
}

@end
